package self.Lunastic.HexaSweeper.Object;

import self.Lunastic.HexaSweeper.Base.UpdateLoop;

/**
 * 游戏计时管理
 */
public class GameTimer {
    // 自动更新对象
    protected UpdateLoop updateLoop = new UpdateLoop(this::update, 10);

    private int _time;
    private final int _timeInit;
    private boolean _running;
    private final boolean _positive;

    /**
     * 用时记录 (正计时)
     */
    public GameTimer(){
        this._timeInit = 0;
        this._positive = true;
        this.updateLoop.set();
        this.reset();
    }

    /**
     * 倒计时
     * @param time 从一个时刻开始
     */
    public GameTimer(int time){
        this._timeInit = time;
        this._positive = false;
        this.updateLoop.set();
        this.reset();
    }

    public void reset(){
        this._time = this._timeInit;
        this._running = false;
    }
    public void run(){ this._running = true; }
    public void stop(){ this._running = false; }
    public void update(){ if(this._running) this._time += (this._positive ? 1 : -1);}

    public int getTime(){ return this._time; }
    public String getTimeText(){ return "" + (this._time / 100) + "." + (this._time % 100); }
}
